use sea_orm_migration::prelude::{sea_query::extension::postgres::Type,*};
use sea_orm::{ EnumIter,DbBackend};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let db = manager.get_connection();
        match db.get_database_backend() {
            DbBackend::MySql | DbBackend::Sqlite => {}
            DbBackend::Postgres => {
                manager
                    .create_type(
                        Type::create()
                            .as_enum(PermissionType::Table)
                            .values([PermissionType::Menu,PermissionType::Button,PermissionType::MenuDir])
                            .to_owned(),
                    )
                    .await?;
                manager
                    .create_type(
                        Type::create()
                            .as_enum(PermissionMenuType::Table)
                            .values([PermissionMenuType::Tab,PermissionMenuType::Link,PermissionMenuType::Iframe])
                            .to_owned(),
                    )
                    .await?;
                manager
                    .create_type(
                        Type::create()
                            .as_enum(PermissionExtend::Table)
                            .values([PermissionExtend::None,PermissionExtend::AddRulesOnly,PermissionExtend::AddMenuOnly])
                            .to_owned(),
                    )
                    .await?;
            }
        }
        manager
            .create_table(
                Table::create()
                    .table(Permission::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Permission::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Permission::Pid).integer())
                    .col(ColumnDef::new(Permission::Type)
                        .enumeration(PermissionType::Table, [PermissionType::Menu,PermissionType::Button,PermissionType::MenuDir])
                    )
                    .col(ColumnDef::new(Permission::Title).string().string_len(50).default(""))
                    .col(ColumnDef::new(Permission::Name).string().string_len(50).default(""))
                    .col(ColumnDef::new(Permission::Path).string().string_len(100).default(""))
                    .col(ColumnDef::new(Permission::Icon).string().string_len(50).default(""))
                    .col(ColumnDef::new(Permission::MenuType)
                        .enumeration(PermissionMenuType::Table, [PermissionMenuType::Tab,PermissionMenuType::Link,PermissionMenuType::Iframe])
                    )
                    .col(ColumnDef::new(Permission::Url).string().string_len(255).default(""))
                    .col(ColumnDef::new(Permission::Keepalive).boolean().not_null().default(false))
                    .col(ColumnDef::new(Permission::Extend)
                        .enumeration(PermissionExtend::Table, [PermissionExtend::None,PermissionExtend::AddRulesOnly,PermissionExtend::AddMenuOnly])
                    )
                    .col(ColumnDef::new(Permission::Remark).text())
                    .col(ColumnDef::new(Permission::Weigh).integer().default(0))
                    .col(ColumnDef::new(Permission::Status).boolean().default(true))
                    .col(ColumnDef::new(Permission::Updatetime).date_time().default(Expr::current_timestamp()))
                    .to_owned(),
            ).await?;
            Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {       
        manager
            .drop_table(Table::drop().table(Permission::Table).to_owned())
            .await?;
        let db = manager.get_connection();

        match db.get_database_backend() {
            DbBackend::MySql | DbBackend::Sqlite => {}
            DbBackend::Postgres => {
                manager
                    .drop_type(Type::drop().name(PermissionType::Table).to_owned())
                    .await?;
                manager
                    .drop_type(Type::drop().name(PermissionMenuType::Table).to_owned())
                    .await?;
                manager
                    .drop_type(Type::drop().name(PermissionExtend::Table).to_owned())
                    .await?;
            }
        }

        Ok(())
    }
}


#[derive(DeriveIden)]
#[allow(dead_code)]
enum Permission {
    Table,
    Id,
    Pid,
    Type,
    Title,
    Name,
    Path,
    Icon,
    MenuType,
    Url,
    Keepalive,
    Extend,
    Remark,
    Weigh,
    Status,
    Updatetime
}

#[derive(Iden, EnumIter)]
pub enum PermissionType {
    Table,
    #[iden = "menu_dir"]
    MenuDir,
    #[iden = "menu"]
    Menu,
    #[iden = "button"]
    Button,
}

#[derive(Iden, EnumIter)]
pub enum PermissionMenuType {
    Table,
    #[iden = "tab"]
    Tab,
    #[iden = "link"]
    Link,
    #[iden = "iframe"]
    Iframe,
}

#[derive(Iden, EnumIter)]
pub enum PermissionExtend {
    Table,
    #[iden = "none"]
    None,
    #[iden = "add_rules_only"]
    AddRulesOnly,
    #[iden = "add_menu_only"]
    AddMenuOnly,
}
